package com.bjesc_b2c.app.appraiset.mvp.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.MainViewAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseFragment;
import com.bjesc_b2c.app.appraiset.bean.ItemViewBean;
import com.bjesc_b2c.app.appraiset.bean.SaleCounBean;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.AllCarActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.AllOrderActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.AwaitTaskActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.CompleteOrderActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.IntentionClueActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.IntentionHighSeas;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.IntentionTouristActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.OrderHighSeas;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.TrackListActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.WaitAuditActivity;
import com.rxretrofit_build.util.CustomSharedPreferences;
import com.rxretrofit_build.util.MapUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import app.pinggushi.bjesc.com.baserecyclerview.adapter.RecycleViewDivider;
import butterknife.Bind;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

import static android.R.attr.version;

/**
 * Created by yidong on 2017/6/21.
 */

public class SellFragment extends BaseFragment implements MultiItemTypeAdapter.OnItemClickListener {
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.iv_return)
    LinearLayout returnLin;
    private Intent mIntent;
    private MainViewAdapter adapter;
    private List<ItemViewBean> list;
    private String[] titles = {"待接单", "待跟进任务", "已完成订单", "全部订单", "意向客源线索", "意向客源待跟进", "意向客源公海", "订单公海", "全部车源"};
    private int[] resID = {R.mipmap.orders_clue, R.mipmap.deal_with_task, R.mipmap.complete_task, R.mipmap.all_order, R.mipmap.intention_clue,
            R.mipmap.intention_clue_await, R.mipmap.high_seas, R.mipmap.order_seas, R.mipmap.all_car};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.layout_fragment, null);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void initView() {
        returnLin.setVisibility(View.GONE);
        mIntent = new Intent();
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        recyclerView.setLayoutManager(manager);
        titleTv.setText(getResources().getText(R.string.sell));
    }

    @Override
    public void initData() {
        String role = CustomSharedPreferences.getString(getActivity(), KeyBean.AUTH_ROLE.valueOf(), KeyBean.FILE_NAME.valueOf());
        list = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            ItemViewBean bean = new ItemViewBean(titles[i], resID[i]);
            list.add(bean);
        }
        if (role.equals("2")){
        list.add(new ItemViewBean("待审核", R.mipmap.await_audit));
        }
        adapter = new MainViewAdapter(getActivity(), R.layout.item_gridview, list);
        adapter.setOnItemClickListener(this);
        RecycleViewDivider divider = new RecycleViewDivider(getActivity(), RecycleViewDivider.GRID, 3, R.color.colorEBEBEB);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(divider);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getSaleCount();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getSaleCount();
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        switch (position) {
            case 0:
                mIntent.setClass(getActivity(), TrackListActivity.class);
                startIntent(mIntent);
                break;
            case 1:
                mIntent.setClass(getActivity(), AwaitTaskActivity.class);
                startIntent(mIntent);
                break;
            case 2:
                mIntent.setClass(getActivity(), CompleteOrderActivity.class);
                startIntent(mIntent);
                break;
            case 3:
                mIntent.setClass(getActivity(), AllOrderActivity.class);
                startIntent(mIntent);
                break;
            case 4:
                mIntent.setClass(getActivity(), IntentionClueActivity.class);
                startIntent(mIntent);
                break;
            case 5:
                mIntent.setClass(getActivity(), IntentionTouristActivity.class);
                startIntent(mIntent);
                break;
            case 6:
                mIntent.setClass(getActivity(), IntentionHighSeas.class);
                startIntent(mIntent);
                break;
            case 7:
                mIntent.setClass(getActivity(), OrderHighSeas.class);
                startIntent(mIntent);
                break;
            case 8:
                mIntent.setClass(getActivity(), AllCarActivity.class);
                startIntent(mIntent);
                break;
            case 9:
                mIntent.setClass(getActivity(), WaitAuditActivity.class);
                startIntent(mIntent);
                break;
        }
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
    //dispatch_complete_count 是 string 完成订单统计
//    dispatch_track_count 是 string 接单线索统计
//    dispatch_follow_count 是 string 待跟进订单统计
//    tou_track_count 是 string 线索意向客源统计
//    tou_follow_count 是 string 待跟进意向客源统计
//    dispatch_follow_count_today 是 string 待跟进订单中今日需要处理的数量
    private void getSaleCount() {
        HashMap<String, Object> map = new MapUtil()
                .put("version", version)
                .put("url", true).build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(getActivity(), map).create(IApiActivityRequest.class).getSaleCount(),
                new BaseSubscriber<TokenBean<SaleCounBean>>(getActivity()) {
                    @Override
                    public void onSuccess(TokenBean<SaleCounBean> o) {

                        if (TextUtils.isEmpty(o.getData().getDispatch_track_count())) {
                            adapter.views.get(0).setVisibility(View.GONE);
                        } else {
                            adapter.views.get(0).setVisibility(View.VISIBLE);
//                          adapter.views.get(0).setBackgroundResource(R.drawable.shape_oval_color46cd8e);
                            adapter.views.get(0).setText(o.getData().getDispatch_track_count());
                        }
                        if (TextUtils.isEmpty(o.getData().getDispatch_follow_count_today())) {
                            adapter.views.get(1).setVisibility(View.GONE);
                        } else {
                            adapter.views.get(1).setVisibility(View.VISIBLE);
//                          adapter.views.get(1).setBackgroundResource(R.drawable.shape_oval_colorfd7c07);
                            adapter.views.get(1).setText(o.getData().getDispatch_follow_count_today());
                        }
                        if (TextUtils.isEmpty(o.getData().getDispatch_complete_count())) {
                            adapter.views.get(2).setVisibility(View.GONE);
                        } else {
                            adapter.views.get(2).setVisibility(View.VISIBLE);
                            adapter.views.get(2).setText(o.getData().getDispatch_complete_count());
//                          adapter.views.get(2).setBackgroundResource(R.drawable.shape_oval_color28a7e1);
                        }
                        if (TextUtils.isEmpty(o.getData().getTou_track_count())) {
                            adapter.views.get(4).setVisibility(View.GONE);
                        } else {
                            adapter.views.get(4).setVisibility(View.VISIBLE);
//                          adapter.views.get(4).setBackgroundResource(R.drawable.shape_oval_color35b247);
                            adapter.views.get(4).setText(o.getData().getTou_track_count());
                        }
                        if (TextUtils.isEmpty(o.getData().getTou_follow_count())) {
                            adapter.views.get(5).setVisibility(View.GONE);
                        } else {
                            adapter.views.get(5).setVisibility(View.VISIBLE);
//                          adapter.views.get(5).setBackgroundResource(R.drawable.shape_oval_colorfd7c07);
                            adapter.views.get(5).setText(o.getData().getTou_follow_count());
                        }
                        if (adapter.getDatas().size() > 9) {
                            if (TextUtils.isEmpty(o.getData().getExamine_count())) {
                                adapter.views.get(9).setVisibility(View.GONE);
                            } else {
                                adapter.views.get(9).setVisibility(View.VISIBLE);
                                adapter.views.get(9).setText(o.getData().getExamine_count());
                            }
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }
}
